package mysql

import "gorm.io/gorm"

type Goods struct {
	gorm.Model
	GoodsTitle string  `gorm:"column:goods_title;type:varchar(100);not null;index;comment:商品标题"`
	GoodsPrice float32 `gorm:"column:goods_price;type:decimal(10,2);not null;comment:商品价格"`
	GoodsStock int64   `gorm:"column:goods_stock;type:int(11);defaut:0;comment:商品库存"`
	GoodsCover string  `gorm:"column:goods_cover;type:varchar(255);default:'';comment:商品封面"`
	Status     int8    `gorm:"column:status;type:tinyint(1);defaut:0;comment:商品状态"`
}

// 发布商品
func (g *Goods) CreateGoods() error {
	return MysqlClient.Create(&g).Error
}

// 商品列表
func (g *Goods) GetGoodsList(keyword string, offset, limit int) (goods []Goods, err error) {
	err = MysqlClient.Where("goods_title like ?", "%"+keyword+"%").Limit(limit).Offset(offset).Find(&goods).Error
	return
}

// 商品详情
func (g *Goods) GetGoodsDetail() (err error) {
	err = MysqlClient.Where("id = ?", g.ID).First(&g).Error
	return
}

// 更新商品库存
func (g *Goods) UpdateGoodsStock() error {
	return MysqlClient.Model(&g).Update("goods_stock", g.GoodsStock).Error
}
